package com.itheima.api.array.demo01;

public class ArrayDemo01 {

    public static void main(String[] args) {

        int[] arr = {11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 120};

        int index = method(arr, 55);

        System.out.println(index);

    }

    public static int method(int[] arr, int key) {

        int minIndex = 0;
        int maxIndex = arr.length - 1;
        int midIndex = (minIndex + maxIndex) / 2;
        while (minIndex <= maxIndex) {

            if(arr[midIndex] == key){
                return midIndex;
            }else if(arr[midIndex] > key){
                maxIndex = midIndex - 1;
            }else{
                minIndex = midIndex + 1;
            }
            midIndex = (minIndex + maxIndex) / 2;

        }

        return -1;

    }

}
